package nsu.grandfinal;

import java.util.Calendar;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

public class AlarmReceive extends BroadcastReceiver {
	
	private int						range;
	private int						mode;
	private int						day;
	private double				latitude;
	private double				longitude;
	private String				featureName;
	private static float	distance;
	private AudioManager	aManager;
	private WifiManager		wManager;
	private Context				mContext;
	private Calendar			calendar;
	
	public void onReceive(Context context, Intent getIntent) {
		setmContext(context);
		featureName = getIntent.getStringExtra("featureName");
		range = getIntent.getIntExtra("range", 50);
		mode = getIntent.getIntExtra("mode", 1);
		latitude = getIntent.getDoubleExtra("Lat", 0);
		longitude = getIntent.getDoubleExtra("Lon", 0);
		day = getIntent.getIntExtra("day", 1);
		calendar = Calendar.getInstance();
		
		wManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		aManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		wManager.setWifiEnabled(true);
		if (day == calendar.get(Calendar.DAY_OF_WEEK)) new distanceCheck().execute("");
	}
	
	public void startPopUp() {
		Intent popupIntent = new Intent(mContext, AlertPopup.class);
		popupIntent.putExtra("mode", mode);
		popupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		mContext.startActivity(popupIntent);
	}
	
	private class distanceCheck extends AsyncTask<String, Integer, Long> {
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			Intent progress = new Intent(mContext, LocationPopup.class);
			progress.putExtra("Lat", latitude);
			progress.putExtra("Lon", longitude);
			progress.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			mContext.startActivity(progress);
		}
		
		@Override
		protected Long doInBackground(String... strData) {
			while (getDistance() == 0) {
				publishProgress();
				SystemClock.sleep(500);
				if (getDistance() == 1) {
					cancel(true);
					break;
				}
			}
			return null;
		}
		
		@Override
		protected void onProgressUpdate(Integer... progress) {
			Log.i("getDistance:", getDistance() + "");
		}
		
		@SuppressWarnings("deprecation")
		@Override
		protected void onPostExecute(Long result) {
			
			if (distance <= range) {
				Toast.makeText(getmContext(), "반경안에 진입", Toast.LENGTH_LONG).show();
				switch (mode) {
					case 1:
						aManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
						break;
					
					case 2:
						aManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
						break;
					
					case 3:
						aManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
						break;
				}
				String 소리 = "";
				switch (mode) {
					case 1:
						소리 = "벨소리";
						break;
					case 2:
						소리 = "진동";
						break;
					case 3:
						소리 = "무음";
						break;
				}
				NotificationManager notifier = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
				Notification notify = new Notification(R.drawable.ic_launcher, "졸업작품", System.currentTimeMillis());
				notify.setLatestEventInfo(mContext, featureName + " " + range + "M", 소리 + "로 변경", null);
				notify.flags = Notification.FLAG_AUTO_CANCEL;
				notify.vibrate = new long[] { 200, 200, 500, 300 };
				notify.number++;
				notifier.notify(1, notify);
			} else {
				startPopUp();
			}
		}
		
		@Override
		protected void onCancelled() {
			super.onCancelled();
			Toast.makeText(getmContext(), "위치검색 실패!", Toast.LENGTH_SHORT).show();
		}
	}
	
	public Context getmContext() {
		return mContext;
	}
	
	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}
	
	public void setDistance(float distance) {
		AlarmReceive.distance = distance;
	}
	
	public float getDistance() {
		return distance;
	}
}